package com.koron.css2.systemManage.servlet;

import com.koron.common.bean.query.OperationQueryBean;
import com.koron.css2.ApplicationConfig;
import com.koron.css2.ServerInterface;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.systemManage.bean.OperationTreeBean;
import com.koron.css2.systemManage.service.IOperationService;
import com.koron.css2.systemManage.service.ServiceFactory;
import com.koron.util.Constant;
import com.koron.util.JsonUtils;
import com.koron.util.TreeUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;

/**
 * 菜单列表
 *
 * @author 59532
 */
public class RoleOperationList implements ServerInterface {

    private static Logger log = LogManager.getLogger(RoleOperationList.class);

    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        MessageBean<List> msg = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "查询成功", List.class);
        try {
            OperationQueryBean operation = JsonUtils.objectToPojo(req.getData(), OperationQueryBean.class);
            if (StringUtils.isEmpty(operation.getRoleCode())) {
                msg.setCode(Constant.MESSAGE_INT_FAIL);
                msg.setDescription("角色不能为空");
                return msg;
            }
            IOperationService operationService = ServiceFactory.getOperationService();
            List<OperationTreeBean> operationTree = operationService.listOPByRole(ApplicationConfig.getAppCustomerAppid(),userInfo, operation.getRoleCode(), true);
            //List<OperationTreeBean> operationTree = operationService.listOPByRole(ApplicationConfig.getAppCustomerAppid(), userInfo, operation.getRoleCode(), false);
            operationTree = TreeUtils.list2TreeList(operationTree, "opCode", "parentOpCode", "children");
            msg.setData(operationTree);
        } catch (Exception e) {
            log.error("查询角色菜单出错：" + e);
            msg.setCode(Constant.MESSAGE_INT_FAIL);
            msg.setDescription("查询角色菜单出错");
        }
        return msg;
    }

}
